package com.tamtamy.jtta;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;

import com.tamtamy.jtta.conf.JTTAProperties;
import com.tamtamy.jtta.constant.Type;
import com.tamtamy.jtta.data.Comment;
import com.tamtamy.jtta.data.CommentList;
import com.tamtamy.jtta.data.ContentFile;
import com.tamtamy.jtta.data.ContentLink;
import com.tamtamy.jtta.data.ContentList;
import com.tamtamy.jtta.data.ContentMessage;
import com.tamtamy.jtta.data.ContentWidget;
import com.tamtamy.jtta.data.GroupDetails;
import com.tamtamy.jtta.data.GroupList;
import com.tamtamy.jtta.data.Metadata;
import com.tamtamy.jtta.data.NetworkDetails;
import com.tamtamy.jtta.data.NetworkList;
import com.tamtamy.jtta.data.Notice;
import com.tamtamy.jtta.data.NoticeList;
import com.tamtamy.jtta.data.OnLineUsersList;
import com.tamtamy.jtta.data.TagDetail;
import com.tamtamy.jtta.data.TagList;
import com.tamtamy.jtta.data.Token;
import com.tamtamy.jtta.data.UserDetail;
import com.tamtamy.jtta.data.UserStatsGenerationDetails;
import com.tamtamy.jtta.data.UserStatsGenerationList;
import com.tamtamy.jtta.data.UserStatsList;
import com.tamtamy.jtta.data.UserTag;
import com.tamtamy.jtta.data.UsersList;
import com.tamtamy.jtta.exception.JTTAException;
import com.tamtamy.jtta.exception.LoadPropertiesException;
import com.tamtamy.jtta.exception.ParsingException;
import com.tamtamy.jtta.exception.UnauthorizedException;
import com.tamtamy.jtta.parser.JSONParser;
import com.tamtamy.jtta.parser.Parser;
import com.tamtamy.jtta.parser.XMLParser;


/**
 * TamTamyAPI class sets up the parameters and handle the calls to the resources via URI statements.<br>
 * Uses the <b>jtta.properties</b> file to load configuration settings and the 
 * <a href="http://hc.apache.org/httpclient-3.x/" target="_new">Apache commons-httpclient</a> to manage 
 * http requests and responses from the resource.
 * 
 * @author Omar "0m4r" Adobati 
 * @version %I%, %G% 
 * 
 */

public class  JTTAClient extends HttpClient {

	/**
	 * Holds the application name that uses jTTA
	 */	
	public static final String APP_PROPERTIES = "conf/jtta.properties";
	private final String DEF_ENCODING = "UTF-8";

	private String type;
	private boolean debug;
	private String version;
	private boolean ssl;
	
	private String username;
	private String password;
	
	private Parser parser;	
	
	/**
	 * Class constructor.
	 * 
	 * @see HttpClient
	 * @see #url
	 * @see #version
	 * @see #type
	 * @see #debug
	 * @see #instances
	 */
	public JTTAClient() throws JTTAException{ 
		super();
		try {
			JTTAProperties properties = new JTTAProperties(APP_PROPERTIES);
			setProperties(properties.getApiUsername(),properties.getApiPassword(), properties.getType(), properties.getVersion(), properties.isDebug(),properties.isSsl());
			setParser(properties.getApiUrl(), properties.getVersion());
		} catch (LoadPropertiesException loadPropertiesException) {
			throw new JTTAException(loadPropertiesException);
		}
	}

	/**
	 * @param url
	 * @param version
	 * @param type
	 * @param debug
	 * @throws JTTAException
	 */
	public JTTAClient(String url, String username, String password, String version, String type, boolean debug, boolean ssl)  throws JTTAException{ 
		super();
		try {
			setProperties( URLEncoder.encode(username, DEF_ENCODING), URLEncoder.encode(password, DEF_ENCODING), type, version, debug, ssl);
		} catch (UnsupportedEncodingException unsupportedEncodingException) {
			throw new JTTAException(unsupportedEncodingException);
		}
		setParser(url, version);
	}

	/**
	 * @param username
	 * @param password
	 * @param type
	 * @param version
	 * @param debug
	 */
	private void setProperties(String username, String password, String type, String version, boolean debug, boolean ssl) {
		this.username = username;
		this.password = password;
		this.type = type;
		this.version = version;
		this.debug = debug;
	}
	
	/**
	 * @param url
	 * @param version
	 * @param type
	 * @param debug
	 * @throws JTTAException
	 */
	private void setParser(String url, String version) throws JTTAException {
		if (type.equalsIgnoreCase(Type.XML)){
			parser = new XMLParser(this, url, debug);
		}
		else if (type.equalsIgnoreCase(Type.JSON)) {
			parser = new JSONParser(this, url, debug);
		}
		else {
			throw new JTTAException("Type " + type + " not allowed");
		}
	}

	/**
	 * Calls the login URI to access the resource to execute the login
	 * @see	com.tamtamy.jtta.data.Token
	 * @param usr	username to login
	 * @param pwd	password to login
	 */	
	public Token token;
	
	public void doLogin() throws JTTAException {
		try {
			token = parser.doLogin(this.username, this.password);
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		}
	}
	
	public void doLogin(String username, String password) throws JTTAException {
		try {
			token = parser.doLogin(URLEncoder.encode(username, DEF_ENCODING), URLEncoder.encode(password, DEF_ENCODING));
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		} catch (UnsupportedEncodingException unsupportedEncodingException) {
			throw new JTTAException(unsupportedEncodingException);
		}
	}
	
	public boolean isValidToken(String username, String token) throws JTTAException {
		try {
			return parser.isValidToken(URLEncoder.encode(username, DEF_ENCODING), token);
		}catch (UnsupportedEncodingException unsupportedEncodingException) {
			throw new JTTAException(unsupportedEncodingException);
		}
	}
	
	/**
	 * Calls the logout URI to access the resource to execute the logout
	 * @throws JTTAException 
	 */
	
	public void doLogout() throws JTTAException {
		try {
			parser.doLogout();
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		}
		
	}

	/**
	 * Calls the URI to access the resource to have back the complete Tag List
	 * @see	com.tamtamy.jtta.data.TagList
	 * @return	com.omar.labs.tamtamy.TagList
	 * @throws JTTAException 
	 */
	public TagList getTagList(long startOffset, long limit) throws JTTAException {	
		TagList tagList = new TagList();
		try {
			tagList = parser.getTagList(startOffset,limit);
			if (debug){
				System.out.println("[getTagList] " +
									"(startOffset:" + startOffset + 
									",limit:" 		+ limit + ")\n" + 
									tagList.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		}
		return tagList;		
	}

	/**
	 * Calls the URI to access the resource to have back the complete Tag Details information
	 * @see	com.tamtamy.jtta.data.TagDetail
	 * @see	com.tamtamy.jtta.data.xml.XMLTagDetail
	 * @param tag	name of the tag
	 * @return	com.omar.labs.tamtamy.TagDetail
	 * @throws JTTAException 
	 */
	public TagDetail getTagDetail(String tag) throws JTTAException {
		TagDetail tagDetail = new TagDetail();
		try {
			tagDetail = parser.getTagDetail(tag);
			if (debug){
				System.out.println("[getTagDetail] " +
									"(tag:" + tag + ")\n" + 
									tagDetail.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		}
		return tagDetail;
	}
	
	/**
	 * Calls the URI to access the resource to have back the complete Content List
	 * @see	com.tamtamy.jtta.data.xml.XMLContentList
	 * @see	com.tamtamy.jtta.data.Content
	 * @see com.tamtamy.jtta.data.ContentFilter
	 * @see com.tamtamy.jtta.data.ContentOrder
	 * @param startOffset starting point int the list
	 * @param limit number of items to display
	 * @param filter 
	 * @param order
	 * @return	com.omar.labs.tamtamy.ContentList
	 * @throws JTTAException 
	 * 
	 */
	public ContentList getContentList(long startOffset, long limit, String filter, String order) throws JTTAException {
		ContentList contentList = new ContentList();
		try {
			contentList = parser.getContentList(startOffset,limit,filter,order);
			if (debug){
				System.out.println("[getContentList] " +
									"(startOffset:" + startOffset + 
									",limit:" 		+ limit + 
									",filter:" 		+ filter + 
									",order:" 		+ order + ")\n" + 
									contentList.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		} catch (ParsingException parsingException) {
			throw new JTTAException(parsingException);
		} 
		return contentList;		
	}			
	
	/**
	 * Calls the URI to access the resource to have back the Content File information
	 * @see	com.tamtamy.jtta.data.ContentFile
	 * @see	com.tamtamy.jtta.data.xml.XMLContentFile
	 * @param	contentID the content ID
	 * @return	com.omar.labs.tamtamy.ContentFile
	 * @throws JTTAException 
	 */
	public ContentFile getContentFile(String contentID) throws JTTAException {
		ContentFile contentFile = new ContentFile();
		try {
			contentFile = parser.getContentFile(contentID);
			if (debug){
				System.out.println("[getContentFile] " +
									"(contentID:" + contentID + ")\n" + 
									contentFile.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		}
		return contentFile;	
	}
	
	/**
	 * Calls the URI to access the resource to have back the Content Link information
	 * @see	com.tamtamy.jtta.data.ContentFile
	 * @see	com.tamtamy.jtta.data.xml.XMLContentLink
	 * @param	contentID the content ID
	 * @return	com.omar.labs.tamtamy.ContentLink
	 * @throws JTTAException 
	 */
	public ContentLink getContentLink(String contentID) throws JTTAException {
		ContentLink contentLink = new ContentLink();
		try {
			contentLink = parser.getContentLink(contentID);
			if (debug){
				System.out.println("[getContentLink] " +
									"(contentID:" + contentID + ")\n" + 
									contentLink.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		}
		return contentLink;	
	}
	
	/**
	 * Calls the URI to access the resource to have back the Content Message information
	 * @see	com.tamtamy.jtta.data.ContentFile
	 * @see	com.tamtamy.jtta.data.xml.XMLContentMessage
	 * @param	contentID the content ID
	 * @return	com.omar.labs.tamtamy.ContentMessage
	 * @throws JTTAException 
	 */
	public ContentMessage getContentMessage(String contentID) throws JTTAException {
		ContentMessage contentMessage = new ContentMessage();
		try {
			contentMessage = parser.getContentMessage(contentID);
			if (debug){
				System.out.println("[getContentMessage] " +
									"(contentID:" + contentID + ")\n" + 
									contentMessage.toString());
			}
		}
		catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		}
		return contentMessage;	
	}
	
	/**
	 * Calls the URI to access the resource to have back the Content Widget information
	 * @see	com.tamtamy.jtta.data.ContentFile
	 * @see	com.tamtamy.jtta.data.xml.XMLContentWidget
	 * @param	contentID the content ID
	 * @return	com.omar.labs.tamtamy.ContentWidget
	 * @throws JTTAException 
	 */
	public ContentWidget getContentWidget(String contentID) throws JTTAException {
		ContentWidget  contentWidget = new ContentWidget();
		try {
			contentWidget = parser.getContentWidget(contentID);
			if (debug){
				System.out.println("[getContentWidget] " +
									"(contentID:" + contentID + ")\n" + 
									contentWidget.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		}
		return  contentWidget;	
	}
	
	/**
	 * Calls the URI to access the resource to have back the Comment List
	 * @see	com.tamtamy.jtta.data.Content
	 * @see	com.tamtamy.jtta.data.ContentList
	 * @see	com.tamtamy.jtta.data.xml.XMLCommentList
	 * @param startOffset starting point
	 * @param 	limit number of items to display
	 * @return	com.omar.labs.tamtamy.CommentList
	 * @throws JTTAException 
	 */
	public CommentList getCommentList(long startOffset, long limit) throws JTTAException {
		CommentList commentList = new CommentList();
		try {
			commentList = parser.getCommentList(startOffset, limit);
			if (debug){
				System.out.println("[getCommentList] " +
									"(startOffset:" + startOffset + 
									",limit:" 		+ limit + ")\n" + 
									commentList.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		} catch (ParsingException parsingException) {
			throw new JTTAException(parsingException);
		} 
		return commentList;	
	}
	
	/**
	 * Calls the URI to access the resource to have back the Comment item
	 * @see	com.tamtamy.jtta.data.Content
	 * @see	com.tamtamy.jtta.data.ContentList
	 * @see	com.tamtamy.jtta.data.xml.XMLComment
	 * @param	contents the content ID
	 * @return	com.omar.labs.tamtamy.Comment
	 * @throws JTTAException 
	 */
	public Comment getComment(long commentID) throws JTTAException {
		Comment comment = new Comment();
		try {
			comment = parser.getComment(commentID);
			if (debug){
				System.out.println("[getComment] " +
									"(commentID:" + commentID + ")\n" + 
									comment.toString());
			}			
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		}
		return comment;	
	}
	
	/**
	 * Calls the URI to access the resource to have back the Notice List
	 * @see	com.tamtamy.jtta.data.Content
	 * @see	com.tamtamy.jtta.data.NoticeList
	 * @see	com.tamtamy.jtta.data.xml.XMLNoticeList
	 * @see com.tamtamy.jtta.data.NoticeContent
	 * @see com.tamtamy.jtta.data.NoticeContentComment
	 * @see com.tamtamy.jtta.data.NoticeBlog
	 * @see com.tamtamy.jtta.data.NoticeWiki
	 * @see com.tamtamy.jtta.data.NoticeForum
	 * @see com.tamtamy.jtta.data.NoticeUser
	 * @see com.tamtamy.jtta.data.NoticeUserTagging
	 * @param 	startOffset starting point
	 * @param 	limit number of items to display
	 * @param 	filter
	 * @return	com.omar.labs.tamtamy.CommentList
	 * @throws JTTAException 
	 */
	public NoticeList getNoticeList(long startOffset, long limit, String filter) throws JTTAException {
		NoticeList noticeList = new NoticeList();
		try {
			noticeList = parser.getNoticeList(startOffset, limit, filter);
			if (debug){
				System.out.println("[getNoticeList] " +
									"(startOffset:" + startOffset + 
									",limit:" 		+ limit + 
									",filter:" 		+ filter + ")\n" + 
									noticeList.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		} catch (ParsingException parsingException) {
			throw new JTTAException(parsingException);
		} 
		return noticeList;	
	}
	
	/**
	 * Calls the URI to access the resource to have back the Notice
	 * @see	com.tamtamy.jtta.data.Content
	 * @see	com.tamtamy.jtta.data.NoticeList
	 * @see	com.tamtamy.jtta.data.xml.XMLNoticeList
	 * @see com.tamtamy.jtta.data.NoticeContent
	 * @see com.tamtamy.jtta.data.NoticeContentComment
	 * @see com.tamtamy.jtta.data.NoticeBlog
	 * @see com.tamtamy.jtta.data.NoticeWiki
	 * @see com.tamtamy.jtta.data.NoticeForum
	 * @see com.tamtamy.jtta.data.NoticeUser
	 * @see com.tamtamy.jtta.data.NoticeUserTagging
	 * @param 	noticeID 
	 * @return	com.omar.labs.tamtamy.CommentList
	 * @throws JTTAException 
	 */
	public Notice getNotice(long noticeID) throws JTTAException {
		Notice notice = new Notice();
		try {
			notice = parser.getNotice(noticeID);
			if (debug){
				System.out.println("[getNotice] " +
									"(noticeID:" + noticeID + ")\n" + 
									notice.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		}
		return notice;	
	}
	
	public UserStatsList getUserStatsList(String userID, long startOffset, long limit) throws JTTAException  {
		UserStatsList userStats = new UserStatsList();
		try {
			userStats = parser.getUserStatsList(userID, startOffset, limit);
			if (debug){
				System.out.println("[getUserStatsList] " +
									"(userID:" 		+ userID + 
									",startOffset:" + startOffset + 
									",limit:" 		+ limit + ")\n" + 
									userStats.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		}		
		return userStats;	
	}
	
	public UserStatsGenerationList getUserStatsGenerationList(long startOffset, long limit) throws JTTAException {
		UserStatsGenerationList userStatsGenerationList = new UserStatsGenerationList();
		try {
			userStatsGenerationList = parser.getUserStatsGenerationList(startOffset, limit);
			if (debug){
				System.out.println("[getUserStatsGenerationList] " +
									"(startOffset:" + startOffset + 
									",limit:" 		+ limit + ")\n" + 
									userStatsGenerationList.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		} catch (ParsingException parsingException) {
			throw new JTTAException(parsingException);
		} 
		return userStatsGenerationList;	
	}
	
	public UserStatsGenerationDetails getUserStatsGenerationDetails(String userStatsID, long startOffset, long limit) throws JTTAException {
		UserStatsGenerationDetails userStatsGenerationDetails = new UserStatsGenerationDetails();
		try {
			userStatsGenerationDetails = parser.getUserStatsGenerationDetails(userStatsID, startOffset, limit);
			if (debug){
				System.out.println("[getUserStatsGenerationDetails] " +
									"(userStatsID:" + userStatsID + 
									",startOffset:" + startOffset + 
									",limit:" 		+ limit + ")\n" + 
									userStatsGenerationDetails.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		}
		return userStatsGenerationDetails;	
	}
	
	public GroupList getGroupList(long startOffset, long limit) throws JTTAException {
		GroupList groupList = new GroupList();	
		try {
			groupList = parser.getGroupList(startOffset, limit);
			if (debug){
				System.out.println("[getGroupList] " +
									"(startOffset:" + startOffset + 
									",limit:" 		+ limit + ")\n" + 
									groupList.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		} catch (ParsingException parsingException) {
			throw new JTTAException(parsingException);
		} 	
		return groupList;	
	}
	
	public GroupDetails getGroupDetails(String groupID) throws JTTAException {
		GroupDetails groupDetails = new GroupDetails();
		try {
			groupDetails = parser.getGroupDetails(groupID);
			if (debug){
				System.out.println("[getGroupDetails] " +
									"(groupID:" + groupID + ")\n" + 
									groupDetails.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		}
		return groupDetails;	
	}
	
	public NetworkList getNetworkList(long startOffset, long limit, String filter) throws JTTAException {
		NetworkList networkList = new NetworkList();
		try {
			networkList = parser.getNetworkList(startOffset, limit, filter);
			if (debug){
				System.out.println("[getNetworkList] " +
									"(startOffset:" + startOffset + 
									",limit:" 		+ limit + 
									",filter:" 		+ filter + ")\n" + 
									networkList.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		} catch (ParsingException parsingException) {
			throw new JTTAException(parsingException);
		} 
		return networkList;	
	}
	
	public NetworkList getNetworkList(long startOffset, long limit) throws JTTAException {
		NetworkList networkList = new NetworkList();
		try {
			networkList = parser.getNetworkList(startOffset, limit);
			if (debug){
				System.out.println("[getNetworkList] " +
									"(startOffset:" + startOffset + 
									",limit:" 		+ limit + ")\n" + 
									networkList.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		} catch (ParsingException parsingException) {
			throw new JTTAException(parsingException);
		} 
		return networkList;	
	}
	
	
	public NetworkList getNetworkListWithChatEnabled(long startOffset, long limit, String filter) throws JTTAException {
		NetworkList networkListWithChatEnabled = new NetworkList();
		try {
			networkListWithChatEnabled = parser.getNetworkListWithChatEnabled(startOffset, limit, filter);
			if (debug){
				System.out.println("[getNetworkListWithChatEnabled] " +
									"(startOffset:" + startOffset + 
									",limit:" 		+ limit + 
									",filter:" 		+ filter + ")\n" + 
									networkListWithChatEnabled.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		} catch (ParsingException parsingException) {
			throw new JTTAException(parsingException);
		} 
		return networkListWithChatEnabled;	
	}
	
	public NetworkList getNetworkListWithChatEnabled(long startOffset, long limit) throws JTTAException {
		NetworkList networkListWithChatEnabled = new NetworkList();
		try {
			networkListWithChatEnabled = parser.getNetworkListWithChatEnabled(startOffset, limit);
			if (debug){
				System.out.println("[getNetworkListWithChatEnabled] " +
									"(startOffset:" + startOffset + 
									",limit:" 		+ limit + ")\n" + 
									networkListWithChatEnabled.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		} catch (ParsingException parsingException) {
			throw new JTTAException(parsingException);
		} 
		return networkListWithChatEnabled;	
	}

	public NetworkDetails getNetworkDetails(String networkID) throws JTTAException {
		NetworkDetails networkDetails = new NetworkDetails();
		try {
			networkDetails = parser.getNetworkDetails(networkID);
			if (debug){
				System.out.println("[getNetworkDetails] " +
									"(networkID:" + networkID + ")\n" + 
									networkDetails.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		}
		return networkDetails;	
	}
	
	public Metadata getMetadata(String url) throws JTTAException {
		Metadata metadata = new Metadata();
		try {
			metadata = parser.getMetadata(url);
			if (debug){
				System.out.println("[getMetadata] " +
									"(url:" + url + ")\n" + 
									metadata.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		} catch (Exception exception) {
			throw new JTTAException(exception);
		}
		return metadata;	
	}
	
	public UserTag getUserTag(String filter) throws JTTAException {
		UserTag userTag = new UserTag();
		try {
			userTag = parser.getUserTag(filter);
			if (debug){
				System.out.println("[getUserTag] " +
									"(filter:" + filter + ")\n" + 
									userTag.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		}
		return userTag;	
	}
	
	public UserTag getUserTag(String userID, String filter) throws JTTAException {
		UserTag userTag = new UserTag();
		try {
			userTag = parser.getUserTag(userID, filter);
			if (debug){
				System.out.println("[getUserTag] " +
									"(userID:" 	+ userID + 
									",filter:" 	+ filter + ")\n" + 
									userTag.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		}
		return userTag;	
	}

	/**
	 * Calls the URI to access the resource to have back the complete User List
	 * @see	com.tamtamy.jtta.data.UsersList
	 * @see	com.tamtamy.jtta.data.xml.XMLUsersList
	 * @return	com.omar.labs.tamtamy.XML.XMLUsersList
	 * @throws JTTAException 
	 */
	public UsersList getUsersList(long startOffset, long limit, String filter) throws JTTAException {
		UsersList userList = new UsersList();
		try {
			userList = parser.getUsersList(startOffset, limit, filter);
			if (debug){
				System.out.println("[getUsersList] " +
									"(startOffset:" + startOffset + 
									",limit:" 		+ limit + 
									",filter:" 		+ filter + ")\n" + 
									userList.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		} catch (ParsingException parsingException) {
			throw new JTTAException(parsingException);
		} 	
		return userList;				
	}

	/**
	 * Calls the URI to access the resource to have back the complete On-Line User List
	 * @see	com.tamtamy.jtta.data.OnLineUsersList
	 * @see	com.tamtamy.jtta.data.xml.XMLOnLineUsersList
	 * @return	com.omar.labs.tamtamy.XML.XMLOnLineUsersList
	 * @throws JTTAException 
	 */ 	 
	public OnLineUsersList getOnLineUsersList(long startOffset, long limit) throws JTTAException {
		OnLineUsersList onLineUsersList = new OnLineUsersList();
		try {
			onLineUsersList = parser.getOnLineUsersList(startOffset, limit);
			if (debug){
				System.out.println("[getOnLineUsersList] " +
									"(startOffset:" + startOffset + 
									",limit:" 		+ limit + ")\n" + 
									onLineUsersList.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		}
		return onLineUsersList;	
	}

	/**
	 * Calls the URI to access the resource to have back the User Detail
	 * @see	com.tamtamy.jtta.data.UserDetail
	 * @see	com.tamtamy.jtta.data.xml.XMLUserDetail
	 * @param userID
	 * @return	com.omar.labs.tamtamy.XML.XMLUserDetail	 
	 * @throws JTTAException 
	 */
	public UserDetail getUserDetail(String userID) throws JTTAException {
		UserDetail userDetail = new UserDetail();
		try {
			userDetail = parser.getUserDetail(userID);
			if (debug){
				System.out.println("[getUserDetail] " +
									"(userID:" + userID + ")\n" + 
									userDetail.toString());
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		}
		return userDetail;	
	}
	
	public void downloadContent(String contentID) throws JTTAException {
		try {
			parser.downloadContent(contentID);
			if (debug){
				System.out.println("[downloadContent] " +
									"(contentID:" + contentID + ")\n");
			}
		} catch (UnauthorizedException unauthorizedException) {
			throw new JTTAException(unauthorizedException);
		}	
	}

	public void setProxy(String hostname, int port, String username, String password) {
		this.getParams().setAuthenticationPreemptive(true);
		HostConfiguration hostConfiguration = this.getHostConfiguration();
		hostConfiguration.setProxy(hostname, port);
		HttpState state = this.getState();
		Credentials proxyCredentials = new UsernamePasswordCredentials(username, password);
		state.setProxyCredentials(AuthScope.ANY, proxyCredentials);
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public boolean isDebug() {
		return debug;
	}

	public void setDebug(boolean debug) {
		this.debug = debug;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}		

}
